﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.IO;


namespace OpenLauncher
{
    class PluginProtector
    {
        public static RSACryptoServiceProvider rsa;

        public static void AssignParameter()
        {
            const int PROVIDER_RSA_FULL = 1;
            const string CONTAINER_NAME = "OpenLaunch";
            CspParameters cspParams;
            cspParams = new CspParameters(PROVIDER_RSA_FULL);
            cspParams.KeyContainerName = CONTAINER_NAME;
            cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
            cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
            rsa = new RSACryptoServiceProvider(cspParams);
        }

        public static byte[] DecryptData(byte[] data2Decrypt)
        {
            AssignParameter();
            byte[] pass = Convert.FromBase64String(Properties.Resources.passphrase);
            string publicPrivateKeyXML = Properties.Resources.opencom;
            DarthEncrypt dc = new DarthEncrypt();
            string m = DecPassphrase("/kvVODDVlLKD8BFKlguO+4l9GiysAIbJKbdP/iYEcDGplkC4u+uztc4P7IU9ORj0KfiU3uKnpzSCZE64Qonwlna1nbvJUbd6o0J8a3XmCJ06XC0OirQdJTiKHb9Vs3hKze+lzLgBBadLq/Yz3BvOkMcfH0OgBmiI18KySxn1ZpdgHN15MEbG7HzJYjgF+xt6Pl16KjAQfCYUKI5LijLlP2A2wWAApdIfLdVYHI158R82RTMAWffK1ydGEHEoHNaAlFU/hu5zaLMoij4LhioYx7qVODXmsOD+3K849PHzqhmSy9GaG8Z0Trd8trO56I2+dGu/6pgELLHcFWNYl76mXes1N1tGI14S7fJkrRlpZ/xsF8TFryBUkCvZ/uIdd70Eczf+k58sp4xEradlj7lBF13QFs8K1xglMNYbyme8k2EDEZFq87DVBkxp/TkMhd3CLZaGnJbgwHxakvIml7ecOA==");
            byte[] gmmsd = Convert.FromBase64String(m);
            string mdgas = Encoding.ASCII.GetString(gmmsd);
            dc.PassPhrase = mdgas;
            string fggg = dc.DecryptString(publicPrivateKeyXML);
            rsa.FromXmlString(fggg);
            byte[] plain = rsa.Decrypt(pass, false);
            string h = System.Text.Encoding.ASCII.GetString(plain);
            dc.PassPhrase = h;
            return dc.DecryptBytes(data2Decrypt);
        }
        public static string DecPassphrase(string data2Decrypt)
        {
            string m = "randompassword";
            DarthEncrypt dc = new DarthEncrypt();
            byte[] bytesToEncode1 = Encoding.UTF8.GetBytes(m);
            string EncodedPassphrase = Convert.ToBase64String(bytesToEncode1);
            dc.PassPhrase = "randompassword";
            dc.SaltValue = EncodedPassphrase + m;
            return dc.DecryptString(data2Decrypt);
        }
    }
}
